package com.tedu.udm.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.udm.pojo.dto.RoleAddNewDTO;
import com.tedu.udm.pojo.vo.RoleListItemVO;
import com.tedu.udm.service.IRoleService;
import com.tedu.udm.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "02. 角色管理模块")
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    public RoleController() {
        log.info("创建控制器对象：RoleController");
    }

    // http://localhost:9878/roles/add-new?username=aa&phone=bb&email=cc
    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(RoleAddNewDTO roleAddNewDTO) {
        log.debug("开始处理【添加角色】的请求，参数：{}", roleAddNewDTO);
        roleService.addNew(roleAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9878/roles/9527/delete
    @ApiOperation("根据id删除角色")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "角色id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除角色失败，尝试删除的角色的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除角色】的请求，参数：{}", id);
        roleService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9878/roles
    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<RoleListItemVO>> list() {
        log.debug("开始处理【查询角色列表】的请求，无参数");
        List<RoleListItemVO> list = roleService.list();
        return JsonResult.ok(list);
    }

}